{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9077d0fc",
   "metadata": {},
   "source": [
    "——————————————————————————\n",
    "\n",
    "* 本学习记录人：林涵萩\n",
    "* 记录时间：2021-11-09\n",
    "* week11\n",
    "* 参考文档：\n",
    "> 1. [python官方文档](https://www.python.org/doc/)\n",
    "> 2. [许老师博客](https://www.cnblogs.com/xuzhichao)\n",
    "\n",
    "——————————————————————————"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3a93543",
   "metadata": {},
   "source": [
    "# 本周学习内容\n",
    "\n",
    "## 1. 用户输入（input）\n",
    "\n",
    "> 1. 数据从 服务端定义 --> 客户端 用户输入数据 的转变\n",
    "> 2. 注意：input需要用户执行完，才可以执行后面的代码\n",
    "\n",
    "* 实践：用户登录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "58a48f2f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账号xiaoming\n"
     ]
    }
   ],
   "source": [
    "username = input(\"请输入您的账号\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cafb300d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiaoming\n"
     ]
    }
   ],
   "source": [
    "print(username)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9b9e25de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的账号：Alex\n",
      "登陆成功\n"
     ]
    }
   ],
   "source": [
    "# 1. 假如有一个用户数据（账号）在数据库 username_data 中\n",
    "\n",
    "username_data = \"Alex\" # 服务端 定义数据\n",
    "\n",
    "username = input(\"请输入你的账号：\") # 客户端 数据（用户输入、用户交互行为等）\n",
    "\n",
    "# 2. 如果用户输入的账号等于数据库账号，登录成功（条件判断）\n",
    "if username == username_data:\n",
    "    print(\"登陆成功\")\n",
    "else:\n",
    "    print(\"您输入的账号有误\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f322bf8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账号xiaoming\n",
      "请输入您的密码3333\n",
      "您输入的账号不存在，请重新输入！\n",
      "请输入您的账号Alex\n",
      "请输入您的密码123456\n",
      "登陆成功\n"
     ]
    }
   ],
   "source": [
    "# 实践：实现用户登录系统（尝试除了账号以外，还要判断密码）\n",
    "# 服务端 定义数据：1. username_data  2. password_data\n",
    "username_data = \"Alex\"\n",
    "password_data = \"123456\"\n",
    "\n",
    "# 思考：用户输入只给三次机会，3次过后，5分钟后再给输入机会\n",
    "while True:\n",
    "    username = input(\"请输入您的账号\")\n",
    "    password = input(\"请输入您的密码\")\n",
    "\n",
    "    # 1. 判断账号\n",
    "    if username == username_data:\n",
    "        # 2. 判断密码\n",
    "        if password == password_data:\n",
    "            print(\"登陆成功\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"密码不正确，请重新输入\")\n",
    "        # 同一判断区域行前空格相同\n",
    "    else:\n",
    "        print(\"您输入的账号不存在，请重新输入！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a99170ef",
   "metadata": {},
   "source": [
    "- **注意冒号**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da3d43d4",
   "metadata": {},
   "source": [
    "## 2. while循环\n",
    "\n",
    "* while True 无限循环！什么时候用？\n",
    "> 1. 当不清楚何时结束循环时使用while True（回顾，当我们清楚循环次数时用for + range）\n",
    "> 2. while True 也可以决定**循环次数**：使用计数器count的方法\n",
    "> 3. while True 也可以根据**条件**退出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "438dce95",
   "metadata": {},
   "outputs": [],
   "source": [
    "# for i in range(无穷大)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b87f5924",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙，缠缠绵绵到天涯 1\n",
      "你是风儿我是沙，缠缠绵绵到天涯 2\n",
      "你是风儿我是沙，缠缠绵绵到天涯 3\n",
      "你是风儿我是沙，缠缠绵绵到天涯 4\n",
      "你是风儿我是沙，缠缠绵绵到天涯 5\n",
      "你是风儿我是沙，缠缠绵绵到天涯 6\n",
      "你是风儿我是沙，缠缠绵绵到天涯 7\n",
      "你是风儿我是沙，缠缠绵绵到天涯 8\n",
      "你是风儿我是沙，缠缠绵绵到天涯 9\n",
      "你是风儿我是沙，缠缠绵绵到天涯 10\n"
     ]
    }
   ],
   "source": [
    "count = 1 # 记录循环次数\n",
    "while True:\n",
    "    print(\"你是风儿我是沙，缠缠绵绵到天涯\",count)\n",
    "    count = count + 1\n",
    "    if count == 11:\n",
    "        break      # 停止循环，跳出循环体（上面一堆叫做循环体）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ce0db7dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账号：3\n",
      "请输入您的密码：3\n",
      "您输入的账号不存在或密码错误，请重新输入！您还有 2 次输入机会\n",
      "请输入您的账号：3\n",
      "请输入您的密码：3\n",
      "您输入的账号不存在或密码错误，请重新输入！您还有 1 次输入机会\n",
      "请输入您的账号：3\n",
      "请输入您的密码：3\n",
      "您已经三次输入不正确，请五秒后再进行尝试！\n",
      "请输入您的账号：Alex\n",
      "请输入您的密码：3\n",
      "您输入的账号不存在或密码错误，请重新输入！您还有 2 次输入机会\n",
      "请输入您的账号：Alex\n",
      "请输入您的密码：3\n",
      "您输入的账号不存在或密码错误，请重新输入！您还有 1 次输入机会\n",
      "请输入您的账号：Alex\n",
      "请输入您的密码：3\n",
      "您已经三次输入不正确，请五秒后再进行尝试！\n",
      "请输入您的账号：Alex\n",
      "请输入您的密码：123456\n",
      "登陆成功\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "username_data = \"Alex\"\n",
    "password_data = \"123456\"\n",
    "\n",
    "count = 3  # 起始数字\n",
    "while True:   \n",
    "    count -= 1 # 等价于count = count + 1\n",
    "    username = input(\"请输入您的账号：\")\n",
    "    password = input(\"请输入您的密码：\")\n",
    "    if username == username_data:\n",
    "        if password == password_data:\n",
    "            print(\"登陆成功\")\n",
    "            break\n",
    "        else:\n",
    "            if count>0:\n",
    "                print(\"您输入的账号不存在或密码错误，请重新输入！您还有\"%s\"次输入机会\"%(count))\n",
    "            else:\n",
    "                print(\"您已经三次输入不正确，请五秒后再进行尝试！\")\n",
    "                time.sleep(5)\n",
    "                count = 3\n",
    "    else:\n",
    "        if count>0:\n",
    "            print(\"您输入的账号不存在或密码错误，请重新输入！您还有\",count,\"次输入机会\")\n",
    "        else:\n",
    "            print(\"您已经三次输入不正确，请五秒后再进行尝试！\")\n",
    "            time.sleep(5)\n",
    "            count = 3     # 次数重置回3次"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5dd05a6",
   "metadata": {},
   "source": [
    "## 3. 格式化文本\n",
    "> 1. 占位符 %s ：s 指 str\n",
    ">> 1. %s 代表 str\n",
    ">> 2. %d 代表 number\n",
    ">> 3. %f 代表float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "837c1b41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:zhichao\n",
      "age:25\n",
      "job:IT\n",
      "salary:30000\n",
      "----- INFO OF zhichao ------\n",
      "name:zhichao\n",
      "age:25\n",
      "job:IT\n",
      "salary:30000\n"
     ]
    }
   ],
   "source": [
    "# 用户详细信息（占位符）：\n",
    "name = input(\"name:\")\n",
    "age = input(\"age:\")\n",
    "job = input(\"job:\")\n",
    "salary = input(\"salary:\")\n",
    "\n",
    "info = '''----- INFO OF %s ------\n",
    "name:%s\n",
    "age:%s\n",
    "job:%s\n",
    "salary:%s'''%(name,name,age,job,salary)# 按照出现的次序依次填写变量\n",
    "\n",
    "# 优势：避免字符串拼接出现的语法错误\n",
    "# 劣势：如果有很多很多占位符，顺序有可能出错，容易出现顺序的差错\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ff8ea103",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:zhichao\n",
      "age:25\n",
      "job:IT\n",
      "salary:35000\n",
      "----- INFO OF zhichao ------\n",
      "name:zhichao\n",
      "age:25\n",
      "job:IT\n",
      "salary:35000\n"
     ]
    }
   ],
   "source": [
    "# 3.字符串的格式化：str.format()\n",
    "name = input(\"name:\")\n",
    "age = input(\"age:\")\n",
    "job = input(\"job:\")\n",
    "salary = input(\"salary:\")\n",
    "\n",
    "info = '''----- INFO OF {姓名} ------\n",
    "name:{姓名}\n",
    "age:{年龄}\n",
    "job:{工作}\n",
    "salary:{薪水}'''.format(年龄 = age,工作 = job,姓名 = name,薪水 = salary)\n",
    "\n",
    "print(info)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
